Blog

Luis Majano

March 09, 2011

Spread the word


Share your thoughts

A few folks have asked how you can leverage MXUnit and MockBox to mock a super class class.  Here is a simple example to showcase this:

Class Parent A

<cfcomponent name="A"> 
<!--- send ---> 
<cffunction access="public" hint="" name="send" output="false" returntype="any"> 
<cfreturn parent="">
</cffunction> 
</cfcomponent> 

Class Parent B

<cfcomponent extends="A" name="B"> 
	<!--- send ---> 
	<cffunction access="public" hint="" name="send" output="false" returntype="any"> 
		<cfargument default="" hint="" name="name" required="true" type="string"> 
		<cfif arguments.name neq ""> 
			<cfreturn name="arguments.name"> 
		<cfelse> 
			<cfreturn>
		</cfif> 
	</cffunction> 
</cfcomponent>

Here is our test case, please note that we use the coldbox package for simplicity, if you are using mockbox standalone, then just follow the docs.

<cfcomponent extends="coldbox.system.testing.BaseTestCase">
<cfscript> 
function setup(){ 
    // create and prepare for mocking. 
    b = getMockBox().prepareMock( createObject("component","B") ); 
} 

function testSend(){ 
    // create a virtual super scope and mock a send method on it 
    mockSuper = getMockBox().createStub().$("send","Mocked Super"); 
    
    // inject the mock super into the variables scope 
    b.$property("super","variables", mockSuper); 
    
    // send method call 
    r = b.send(); 
    
    // use simple mxunit debug to show results. 
    debug(r); 
} 
</cfscript>
</cfcomponent>

So as you can see, the super scope is just another reference in the variables scope, so I can easily create a MockBox stub and mock away. You can also be more concise if you want to leave the methods intact in the super scope by creating a mock of the parent class:

getMockBox().createMock("A").$("send","Mocked Super");

 

Add Your Comment

(2)

Mar 10, 2011 09:03:09 UTC

by Chris Tierney

Thanks Luis for this great write up! What is you opinion on mocking a super class? Should it be mocked as the example provides or should it generally be treated as an all-in-one?

May 14, 2013 12:43:43 UTC

by duncan

Thanks for this useful post. It's a shame the blog has screwed up the layout of the code block. Also the date of the post is reading as 1111 for the year

Recent Entries

TestBox Latest Updates and News!

TestBox Latest Updates and News!

We’re thrilled to have launched the new TestBox website and TestBox 6.0! If you haven’t had a chance to explore yet, visit TestBox to discover updated documentation, powerful resources, and features that make testing more efficient than ever.

Maria Jose Herrera
Maria Jose Herrera
November 21, 2024
Is Your ColdFusion Application Ready for the Future?

Is Your ColdFusion Application Ready for the Future?

In a rapidly evolving digital world, maintaining performance, security, and scalability for ColdFusion applications is more challenging than ever. Whether you're using Lucee or Adobe ColdFusion, legacy systems can become a bottleneck for growth, innovation, and user satisfaction. The need to future-proof your ColdFusion applications has never been more critical.

But where do you start?


The Hidden Costs of an Outdated ColdFusion Application

As you...

Cristobal Escobar
Cristobal Escobar
November 21, 2024
The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management

The Hidden Costs of In-House Database Management


Opting for in-house database management involves more than just a salary. Here are some often-overlooked costs associated with maintaining your own DBA team.



1. High Salaries and Benefits


Hiring skilled DBAs is expensive. According to industry reports, the average salary of a DBA in the U.S. can range from $85,000 to over $130,000 per year, depending on experience and expertise. When you add ...

Cristobal Escobar
Cristobal Escobar
November 20, 2024